<fieldset style="width:800px;border:solid 1px #A4BED4;margin-bottom:10px;margin-left:-1px;position:relative;">
    <legend>检索条件</legend>
    <div>
        <table style="width:100%;table-layout:fixed;">
            <tbody>
            <tr id="edit_boxs">
                <td style="width:80px;">选择客户：</td>
                <td style="width:150px;"><input type="hidden" id="customer2_id" name="customer2_id"><input id="customer2" name="customer2" class="text"></td>
                <td style="width:100px;">收款状态：</td>
                <td style="width:150px;">
                    <select name="status2" id="status2" style="width: 130px;">
                        <option value="0">请选择</option>
                        <option value="1">未收款</option>
                        <option value="2">已收款</option>
                    </select>
                </td>

            </tr>
            <tr>
                <td>开始时间：</td>
                <td><input id="start_time" class="date" style="width:130px"></td>
                <td>截至时间：</td>
                <td><input id="end_time" class="date" style="width:130px"></td>
                <td colspan="2" style="width: 100px;"><a href="javascript:void(0)" id="select" class="button"><span class="button-text">检索...</span></a></td>
            </tr>
            </tbody>
        </table>
    </div>
</fieldset>
<div id="w" class="easyui-window" data-options="title:'选择维护费用客户',iconCls:'icon-save',closed:'true'" style="width:700px;height:350px;padding:5px;">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center',border:false" style="padding:10px;background:#fff;border:1px solid #ccc;">
            <fieldset style="width:680px;border:solid 1px #A4BED4;margin-bottom:10px;margin-left:-1px;position:relative;">
                <legend>检索条件</legend>
                <div>
                    <table style="width:100%;table-layout:fixed;">
                        <tbody>
                        <tr id="edit_boxs">
                            <td style="width:100px;">所在区域：</td>
                            <td style="width:150px;"><input id="area" class="easyui-combotree" value="" data-options="url:'/oa/getArea'" style="width:130px;"></td>

                            <td style="width:40px;">名称：</td>
                            <td style="width:150px;"><input id="key" name="key" class="text"></td>
                            <td colspan="2" style="width: 100px;"><a href="javascript:void(0)" id="selectCustomer" class="button"><span class="button-text">检索...</span></a></td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <table id="aftermarket_customer"></table>
        </div>
        <div data-options="region:'south',border:false" style="text-align:right;padding:5px 0;">
            <a class="easyui-linkbutton" id="sele_ok" data-options="iconCls:'icon-ok'" href="javascript:void(0)">Ok</a>
        </div>
    </div>
</div>

<table id="aftermarket"></table>
<fieldset style="width:1065px;border:solid 1px #A4BED4;margin-bottom:10px;margin-top:10px;margin-left:-1px;position:relative;">
    <legend>维护费用管理操作</legend>
    <form action="/oa/aftermarketAdd" method="post" enctype="multipart/form-data" id="aftermarketSubmit">
        <div>
            <input type="hidden" name="id" id="ClickRowID" value="">
            <input type="hidden" name="customer_id" id="customer_id" value="">
            <table style="width:100%;table-layout:fixed;">
                <tbody>
                <tr>
                    <td style="width:100px;">选择客户：</td>
                    <td style="width:445px;">
                        <input name="customer_name" style="width: 250px;" id="customer" class="text">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">负责人：</td>
                    <td style="width:445px;">
                        <input name="staff" style="width: 250px;" class="text required" id="staff" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">款项名称：</td>
                    <td style="width:445px;">
                        <input name="title" style="width: 250px;" class="text required" id="title" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">维护开始时间：</td>
                    <td style="width:445px;">
                        <input name="start_time" style="width: 250px;" id=start_time" class="text date" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">维护结束时间：</td>
                    <td style="width:445px;">
                        <input name="end_time" style="width: 250px;" id="end_time" class="text date" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">维护费用时间：</td>
                    <td style="width:445px;">
                        <input name="must_time" style="width: 250px;" id="must_time" class="text date" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">实际收款时间：</td>
                    <td style="width:445px;">
                        <input name="reality_time" style="width: 250px;" id="reality_time" class="text date" required="true">
                    </td>
                </tr>

                <tr>
                    <td style="width:100px;">收款状态：</td>
                    <td style="width:445px;">
                        <input type="radio" name="status" checked="checked" value="未收款">未收款&nbsp;
                        <input type="radio" name="status" value="已收款">已收款
                    </td>
                </tr>
                <tr><td colspan="2">&nbsp;</td></tr>

                <tr>
                    <td style="width: 100px;">&nbsp;</td>
                    <td style="width: 645px;">
                        <a href="javascript:void(0)" class="button" id="SaveButton">
                            <span class="button-text">提交</span></a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="button" id="ResetButton">
                            <span class="button-text">重置</span>
                        </a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </form>
</fieldset>

<script type="text/javascript">
 var CURTYPE = 'customer2';
 $(document).ready(function(){
    $('#aftermarket_customer').datagrid({
        width:680,
        height:250,
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        url:'/oa/customerdata',
        sortName: 'id',
        remoteSort: false,
        queryParams:{},
        idField:'id',
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        pageList:[10,15,20],
//        frozenColumns:[[
//            {field:'ck',checkbox:true},
//        ]],
        columns:[[
            {field:'id',title:'编号',width:80},
            {field:'name',title:'客户',width:325},
            {field:'area',title:'地区',width:80,rowspan:2}
        ]]
    });
    $("#customer,#customer2").focus(function(){
        CURTYPE = $(this).attr('id');
        $('#w').window('open');
    });

    // 初期化DataGrid
    $('#aftermarket').datagrid({
        width:1080,
        height:250,
        nowrap: true,
        autoRowHeight: false,
        striped: true,
        url:'/oa/aftermarketPattern',
        sortName: 'id',
        remoteSort: false,
        queryParams:{},
        idField:'id',
        pagination:true,
        rownumbers:true,
        pageList:[10,15,20],
        frozenColumns:[[
            {field:'ck',checkbox:true},
        ]],
        columns:[[
            {field:'id',title:'维护费用编号',width:80},
            {field:'title',title:'款项标题',width:325},
            {field:'status',title:'状态',width:80,rowspan:2},
            {field:'staff_name',title:'负责人',width:120},
            {field:'customer_name',title:'客户',width:180},
            {field:'start_time',title:'维护开始时间',width:100},
            {field:'end_time',title:'维护结束时间',width:100},
            {field:'must_time',title:'维护费应收款时间',width:100},
            {field:'reality_time',title:'实际收款时间',width:100}
        ]],
        toolbar:
                [{
                    id:'btnadd',
                    text:'新增维护费用',
                    iconCls:'icon-add',
                    handler:function(){
                        var rows = $('#aftermarket').datagrid('getSelections');
                        if( rows.length == 1 ){
                            var rowData = rows[0];
                            $('#ClickRowID').val();
                            $('#customer_id').val(rowData.customer_id);
                            $('#customer').val(rowData.customer_name);
                            $('#title').val(rowData.title);
                            $("input[name='staff']").val(rowData.staff_id);
                            $("#staff").next("span").find(".combo-text").val(rowData.staff_name);
                            $("#start_time").next().find("input").val(rowData.start_time);
                            $("#end_time").next().find("input").val(rowData.end_time);
                            $("#must_time").next().find("input").val(rowData.must_time);
                            $("#reality_time").next().find("input").val(rowData.reality_time);
                            $('#SaveButton span').text('新增');
                        }else{
                            parent.$.dooDialog.alert('您只能复制一条记录，请重新选择......');
                        }
                    }
                },'-',{
                    text:'删除选中维护费用',
                    iconCls:'icon-remove',
                    handler:function(){
                        var ids  = "";

                        parent.$.dooDialog.confirm("你确定要删除这个维护费用吗？",function(){
                            var rows = $('#aftermarket').datagrid('getSelections');
                            if( rows.length > 0 ){
                                for(var i=0;i<rows.length;i++){
                                    if(i==0){
                                        ids = rows[i].id;
                                    }else{
                                        ids = ids + "," + rows[i].id;
                                    }
                                }

                                $.post('/oa/aftermarketDel',{deleted:ids},function(data){
                                    if(data.err == 0){
                                        $("#aftermarket").datagrid('reload');
                                    }else{
                                        parent.$.dooDialog.alert('维护费用删除失败！');
                                    }
                                },'json');
                            }else{
                                parent.$.dooDialog.alert('您还没有选择要删除的记录，请选择......');
                            }
                        });
                    }
                },'-',{
                    text:'编辑选中维护费用',
                    iconCls:'icon-edit',
                    handler:function(){
                        var rows = $('#aftermarket').datagrid('getSelections');
                        if( rows.length == 1 ){
                            var rowData = rows[0];
                            $('#ClickRowID').val(rowData.id);
                            $('#ClickRowID').val();
                            $('#customer_id').val(rowData.customer_id);
                            $('#customer').val(rowData.customer_name);
                            $('#title').val(rowData.title);
                            $("input[name='staff']").val(rowData.staff_id);
                            $("#staff").next("span").find(".combo-text").val(rowData.staff_name);
                            $("#start_time").next().find("input").val(rowData.start_time);
                            $("#end_time").next().find("input").val(rowData.end_time);
                            $("#must_time").next().find("input").val(rowData.must_time);
                            $("#reality_time").next().find("input").val(rowData.reality_time);
                            $('#SaveButton span').text('保存');
                        }else{
                            parent.$.dooDialog.alert('您只能编辑一条记录，请重新选择......');
                        }
                    }
                }],
        onUnselect:function(rowIndex, field, value){
            $('#ClickRowID').val();
            $('#ClickRowName').val('');
            $('#ClickRowTypes').val('');
            $('#ClickRowRemark').val('');
        }
    });

    // 下拉框设置
    $('#staff').combogrid({
        panelWidth:350,
        panelHeight:250,
        rownumbers:true,
        idField:'id',
        textField:'name',
        url:'/oa/staffPattern/',
        pagination:true,
        pageList:[10],
        columns:[[
            {field:'id',title:'ID',width:150},
            {field:'name',title:'姓名',width:154}
        ]]
    });

    // 检索功能
    $('a#select').click(function(){
        var queryParams = $('#aftermarket').datagrid('options').queryParams;
        queryParams.customer_id = $('#customer2_id').val();
        queryParams.status2 = $('#status2').val();
        queryParams.start_time = $('#start_time').next("span").find("input[type='hidden']").val();
        queryParams.end_time = $('#end_time').next("span").find("input[type='hidden']").val();

        $('#aftermarket').datagrid('options').queryParams=queryParams;
        $("#aftermarket").datagrid('reload');
    });


    // 变更提交
    $('a#SaveButton').click(function(){
        // 数据检测
        var isValid = false;
        if ($("#title").val() == '') {
            isValid = true;
            return false;
        }

        // 检测结果
        if(isValid){
            return false
        }

        // 数据提交
        $.post($('form#aftermarketSubmit').attr('action'),$('form#aftermarketSubmit').serializeArray(),function(data){
            if (data.err == 0){
                $("#aftermarket").datagrid('reload');
                $('input.text,input.number,input.count').each(function(){
                    $(this).val('');
                });
                $("#title").val('');
                $('#ClickRowRemark').val('');
                parent.$.dooDialog.alert('维护费用记录成功！');
                return true;
            }else{
                parent.$.dooDialog.alert(data.err);
                return false;
            }
        },'json');
    });

    // 重置
    $('a#ResetButton').click(function(){
        $('input.text,input.number,input.count').each(function(){
            $(this).val('');
        });

        $('input[name="title"]').val('');
        $('input[name="content"]').val('');
        $('#ClickRowRemark').val('');
    });


    // 客户检索功能
    $('a#selectCustomer').click(function(){
        var queryParams = $('#aftermarket_customer').datagrid('options').queryParams;
        queryParams.area = $('#area').next("span").find("input[type='hidden']").val();
        queryParams.key = $('#key').val();

        $('#aftermarket_customer').datagrid('options').queryParams=queryParams;
        $("#aftermarket_customer").datagrid('reload');
    });
    $("#sele_ok").click(function(){
        var rows = $('#aftermarket_customer').datagrid('getSelections');
        if (rows.length == 0) {
            parent.$.dooDialog.alert('请选择一条记录......');
            // return false;
        }
        if (CURTYPE == 'customer2') {
            $("#customer2_id").val(rows[0].id);
            $("#customer2").val(rows[0].name);
        } else {
            $("#customer_id").val(rows[0].id);
            $("#customer").val(rows[0].name);
        }

        $('#w').window('close');
    });

});
</script>